<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Messages
 *
 * @author Admin
 */
class Messages
{
	/**
	 *
	 * @var My_Messages
	 */
	protected static $_instance = null;
	
	/**
	 *
	 * @var array
	 */
	protected $_messages = array();

	/**
	 *
	 * @var CHttpSession
	 */
	protected $_session = null;

	private function __construct()
	{
		$this->_session = new CHttpSession();
		if( $this->_session->contains('messages') )
			$this->_messages = $this->_session->get('messages');
		else
			$this->_messages = array();
	}

	/**
	 *
	 * @return Messages
	 */
	public static function getInst()
	{
		if( !self::$_instance )
			self::$_instance = new self;
		
		return self::$_instance;
	}
	
	public function addMessage($key, $value)
	{
		$this->_messages[$key] = $value;
		
		$this->_session->add('messages', $this->_messages);
		
		return $this;
	}
	
	public function getMessage( $key )
	{
		if( !$key || !isset( $this->_messages[$key] ) )
			return null;
		
		return $this->_messages[$key];
	}
	
	public function getMessages()
	{
		return $this->_messages;
	}
	
	public function clear()
	{
		$this->_messages = array();
		$this->_session->remove('messages');
		
		return $this;
	}
}